package com.yunxi.service.dubboService.impl.otd;

import com.yunxi.bizToBaseData.service.Biz2BaseVehicleConfigDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.hubToBiz.service.ProduceModelCheckDubboService;
import com.yunxi.model.otd.VehicleConfigConstraint;
import com.yunxi.service.localService.VehicleProductViewService;
import com.yunxi.service.localService.otd.OtdModelProduceConfigService;
import com.yunxi.service.localService.otd.VehicleConfigConstraintService;
import com.yunxi.vehicle.pojo.VehicleProductViewVO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 投产车型校验
 *
 * @author XUQI
 */
public class ProduceModelCheckDubboServiceImpl implements ProduceModelCheckDubboService {

    private static final Logger logger = LoggerFactory.getLogger(ProduceModelCheckDubboServiceImpl.class);


    @Autowired
    @Qualifier("vehicleConfigConstraintServiceImpl")
    VehicleConfigConstraintService configConstraintService;

    @Autowired
    Biz2BaseVehicleConfigDubboService biz2baseVehicleConfigDubboService;

    @Autowired
    @Qualifier("otdModelProduceConfigServiceImpl")
    OtdModelProduceConfigService modelProduceConfigService;

    @Autowired
    @Qualifier("vehicleProductViewServiceImpl")
    VehicleProductViewService productService;

    /**
     * 检查是否匹配约束规则
     *
     * @param vehicleCode 15位
     * @param inColor 内饰
     * @param outColor 外饰
     * @param packages 选装包 XZAB,XZBA
     * @return true 是  false 否
     */
    @Override
    public ApiResult check(String vehicleCode, String inColor, String outColor, String packages) {
        try {
            // 查询产品信息
            VehicleProductViewVO product = productService.findProductBySub(vehicleCode, inColor, outColor);
            if (product == null) {
               return ResponseUtil.fail(-1, "未找到产品信息：[" + "配置" + vehicleCode + "内饰" + inColor + "外饰" + outColor + "]");
            }

            ////// 约束条件
            Map<String, List<VehicleConfigConstraint>> constraintsMap = configConstraintService
                .findEnableConfigConstraintMap();

            // 当前车型对应约束规则
            List<VehicleConfigConstraint> constraintList = constraintsMap.get(product.getModelCode());

            ////// 全量工程配置
            // 全量工程配置 MAP,<key:特征值,value:特征族>
            Map<String, String> allConfigMap = biz2baseVehicleConfigDubboService.loadAllEngineeringConfig();

            //// =======2.工程配置
            /////// 根据约束关系调整默认工程配置 ///////
            // 单车默认配置MAP,<key:特征族,value:特征值>
            Map<String, String> configMap = biz2baseVehicleConfigDubboService
                .loadEngineeringConfigByVehicleCode(vehicleCode, inColor, outColor, packages, allConfigMap);

            // 验证约束规则
            if (!CollectionUtils.isEmpty(constraintList)) {
                configConstraintService.replaceConfig(constraintList, configMap, allConfigMap);
            }

            // 查询颜色关联的必选选装包
            List<String> colorCode = new ArrayList<>();
            colorCode.add(inColor);
            colorCode.add(outColor);
            Map<String, List<Object>> requiredMap = configConstraintService
                .findRequiredPackagesByFeatureCode(colorCode, vehicleCode);
            // requiredSelect：必选的选装包
            // notSelect：不可手动选的选装包
            List<Object> requiredSelect = new ArrayList<>();
            List<Object> notSelect = new ArrayList<>();
            if (requiredMap != null) {
                requiredSelect = requiredMap.get("requiredSelect");
                notSelect = requiredMap.get("notSelect");
            }

            List<String> packageCodeList = new ArrayList<>();

            if (!StringUtils.isEmpty(packages)) {
                packages = packages == null ? "" : packages;
                String[] packageCodes = packages.split(",");
                packageCodeList = Arrays.asList(packageCodes);
                //验证选装包是否互斥
                boolean bool = configConstraintService.checkExcludeCustomPackCode(packageCodeList, vehicleCode);
                if (!bool) {
                    logger.debug("ProduceModelCheckDubboServiceImpl.check: 选装包互斥" + packages);
                    return ResponseUtil.fail(-1, "选装包互斥" + packages);
                }
            }

            //// 验证必选选装包是否选择
            if (!CollectionUtils.isEmpty(requiredSelect)) {
                for (Object obj : requiredSelect) {
                    if (!packageCodeList.contains((String) obj)) {
                        logger.debug("ProduceModelCheckDubboServiceImpl.check: 缺少必选选装包" + obj);
                        return ResponseUtil.fail(-1,"缺少必选选装包" + obj);
                    }
                }
            }

            if (!CollectionUtils.isEmpty(notSelect)) {
                // 验证必选选装包是否不应该选
                for (Object obj : notSelect) {
                    if (!requiredSelect.contains(obj)) {
                        if (packageCodeList.contains((String) obj)) {
                            logger.debug("ProduceModelCheckDubboServiceImpl.check: 选装包不可选" + obj);
                            return ResponseUtil.fail(-1, "选装包不可选" + obj);
                        }
                    }
                }
            }

//            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("ProduceModelCheckDubboServiceImpl.check:" + e.getMessage());
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }
}

